package tree;

import java.util.*;

public class binaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }


    public TreeNode createTree(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');


        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }
    //前序遍历
    void preOrder(TreeNode root){
       if (root==null){
           return;
       }
       System.out.print(root.val+" ");
       preOrder(root.left);
       preOrder(root.right);
    }
    void inOrder(TreeNode root){
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    void postOrder(TreeNode root){
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    // 获取树中节点的个数
    int size(TreeNode root){
        if (root==null){
            return 0;
        }
        return size(root.left)+size(root.right)+1;

    }
    public int nodeSize=0;
    int size2(TreeNode root){
        if (root==null){
            return 0;
        }
        nodeSize++;
        size2(root.left);
        size2(root.right);
        return nodeSize;
    }
    // 获取叶子节点的个数
    int getLeafNodeCount(TreeNode root){
        if (root==null){
            return 0;
        }
        if (root.left==null&&root.right==null){
            return 1;
        }
        return getLeafNodeCount(root.left)+getLeafNodeCount(root.right);
    }
    //求树的高度
    int getHeight(TreeNode root){
        if (root==null){
            return 0;
        }
        int leftHeight=getHeight(root.left);
        int rightHeight=getHeight(root.right);
        return leftHeight>rightHeight?leftHeight+1:rightHeight+1;
    }
    TreeNode find(TreeNode root,char val){
        if (root==null){
            return null;
        }
        if (root.val==val){
            return root;
        }
        TreeNode ret1=find(root.left,val);
        if (ret1!=null){
            return ret1;
        }
        TreeNode ret2=find(root.right,val);
        if (ret2!=null){
            return ret2;
        }
        return null;
    }
    //判断是不是两颗相同的树
    /*
    1.判断时间复杂度
    min（m，n）


     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //首先判断两棵树的结构是否为空
        if((p!=null&&q==null)||(p==null&&q!=null)){
            return false;
        }
        //如果两棵树都为空
        if(p==null&&q==null){
            return true;
        }
        //判断值是否一样
        if(p.val!=q.val){
            return false;
        }

        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }

    //判断一棵树是不是另一颗树的子树
    /*
    1.因为每一个节点都需要和另一棵树的每一个节点做比较所以
    时间复杂度为：m*n
     */
    public boolean isSubtree(TreeNode root,TreeNode subRoot){
        //防止root空指针异常
        if (root==null||subRoot==null){
            return false;
        }
        //1.判断是不是根节点
        if (isSameTree(root,subRoot)){
            return true;
        }
        //2.判断是不是他的左子树
        if (isSubtree(root.left,subRoot)){
            return true;
        }
        if (isSubtree(root.right,subRoot)){
            return true;
        }
        return false;
    }
    class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root==null){
                return true;
            }
            return isSymmetricChild(root.left,root.right);
        }


        private boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree){

            if (leftTree!=null&&rightTree==null||leftTree==null&&rightTree!=null){
                return false;
            }
            if (leftTree==null&&rightTree==null){
                return true;
            }
            if (leftTree.val!=rightTree.val){
                return false;
            }
            if ((leftTree.left==rightTree.right&&leftTree.right==rightTree.left)&&leftTree.val==rightTree.val){
                return true;
            }
            return isSymmetricChild(leftTree.left,rightTree.right)&&
            isSymmetricChild(leftTree.right,rightTree.left);

        }
        //层序遍历


    }
    void levelOrder1(TreeNode root){
        //利用队列将节点存储再遍历
        if (root==null){
            return;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if (cur.left!=null){
                queue.offer(cur.left);
            }
            if (cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }

    public List<List<Integer>> levelOrder(TreeNode root){
        List<List<Integer>> ret=new ArrayList<>();
        if (root==null){
            return null;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size=queue.size();
            List<Integer> tmp=new ArrayList<>();
            while (size!=0){
                TreeNode cur=queue.poll();
                size--;
                tmp.add(cur.val);
                if (cur.left!=null){
                    queue.offer(cur.left);
                }
                if (cur.right!=null){
                    queue.offer(cur.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }
        //
    /*
    //创建二叉树
    import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val = val;
    }
}
public class Main {
    public static int i=0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();

            TreeNode root = createTree(str);

            inorder(root);
        }
    }
    public static TreeNode createTree(String str) {
        //创建一个节点，
        TreeNode root=null;
        //如果遇到的不是#就存储
        if(str.charAt(i)!='#'){
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=createTree(str);
            root.right=createTree(str);

        }else{
            i++;
        }
            return root;
    }
    public static void inorder(TreeNode root) {
        if (root == null) {
            return ;
        }
        inorder(root.left);

        System.out.print(root.val + " ");

        inorder(root.right);

    }

}
     */




    //最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;

        if(root==p||root==q){
            return root;
        }
        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);

        if(leftTree!=null&&rightTree!=null){
            return root;
        }else if(leftTree!=null){
            return leftTree;
        }else{
            return rightTree;
        }
    }


    //非递归的中序遍历
    void Inorder2(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while (cur!=null||!stack.isEmpty()){
            while (cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.println(top.val);
            cur=top.right;
        }
    }
    //非递归的前序遍历
    void preorder2(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while (cur!=null||!stack.isEmpty()){
            while (cur!=null){
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }
            TreeNode top=stack.pop();

            cur=top.right;
        }
    }

    
//反转一颗二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        if(root.left==null&&root.right==null){
            return root;
        }
        TreeNode tmp=root.left;
        root.left=root.right;
        root.right=tmp;
        invertTree(root.left);
        invertTree(root.right);

        return root;
    }
}
